package com.kmproject.haifu.mvp.presenter;

import android.app.Activity;
import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.MyStorySelectNewContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.MemberCenterUserInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoryBean;
import com.kmproject.haifu.mvp.ui.adapter.MyStoryXZDPAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/01/2019 10:45
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class MyStorySelectNewPresenter extends BasePresenter<MyStorySelectNewContract.Model, MyStorySelectNewContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MyStorySelectNewPresenter(MyStorySelectNewContract.Model model, MyStorySelectNewContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 获取用户会员中心信息
     */
    public void getMemberLevelByUser() {
        mModel.getMemberLevelByUser().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<MemberCenterUserInfoBean>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<MemberCenterUserInfoBean> memberCenterUserInfoBeanResultBean) {
                        if (AppUtil.isSuccess(memberCenterUserInfoBeanResultBean)) {
                            mRootView.getMemberLevelByUserSuccess(memberCenterUserInfoBeanResultBean.getData());
                        } else {
                            mRootView.showMessage(memberCenterUserInfoBeanResultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 移除店铺
     * @param storeId
     */
   public void  removeStore(String storeId)
   {
       Map<String,Object> maps=new HashMap<>();
       maps.put("type","1");
       mModel.removeStore(Api.APP_DOMAIN+"/app/userStore/removeStore/"+storeId,maps).compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                   @Override
                   public void onNext(ResultBean resultBean) {
                    if (AppUtil.isSuccess(resultBean))
                    {
                        mRootView.removeStoreSuccess();
                    }else {
                        mRootView.showMessage(resultBean.getMessage());
                    }
                   }
               });
   }
    /**
     * 开启店铺
     * @param myStoryXZDPAdapter
     */
    public void enableStore(Activity activity,MemberCenterUserInfoBean memberCenterUserInfoBean, MyStoryXZDPAdapter myStoryXZDPAdapter){
        if (myStoryXZDPAdapter==null||myStoryXZDPAdapter.getData().isEmpty())
        {
            mRootView.showMessage("没有可开启的店铺");
            return;
        }
        List<String> ids=getEnableStoreIds(myStoryXZDPAdapter.getData());
        if (ids.isEmpty())
        {
            mRootView.showMessage("请选择要开启的店铺");
            return;
        }
//        if (memberCenterUserInfoBean.getStoreTotal()!=-1&&(memberCenterUserInfoBean.getIsShowStoreCount()+ids.size())>memberCenterUserInfoBean.getStoreTotal())
        if (memberCenterUserInfoBean.getCanShowStoreCount()!=-1&&ids.size()>memberCenterUserInfoBean.getCanShowStoreCount())
        {
            //已选店铺数>当前会员等级店铺数量，继续选择店铺toast提示“您当前只可以开启xxx个店铺哦！”，3s后消失；
            ArmsUtils.makeText(activity,"您当前只可以开启"+memberCenterUserInfoBean.getCanShowStoreCount()+"个店铺哦！");
            return;
        }
        new AlertView("", "确认开启后不能更换其他店铺经营哦！", "我知道了", new String[]{"确认开启"}, null, activity, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

            @Override
            public void onItemClick(Object o, int position) {
                if (position == AlertView.CANCELPOSITION) {

                }else {
                    Map<String,Object> idsMap=new HashMap<>();
                    idsMap.put("storeIds",ids);
                    mModel.enableStore(idsMap).compose(RxUtils.applySchedulers(mRootView))
                            .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                                @Override
                                public void onNext(ResultBean resultBean) {
                                    if (AppUtil.isSuccess(resultBean))
                                    {
                                        mRootView.enableStoreSuccess(ids.size());
                                    }else {
                                        mRootView.showMessage(resultBean.getMessage());
                                    }
                                }
                            });
                }

            }
        },ArmsUtils.getColor(activity, R.color.alert_button_00adfcr),true).show();


    }

    /**
     * 获取开启店铺的id
     * @param myStoryBeans
     * @return
     */
    public List<String> getEnableStoreIds(List<MyStoryBean> myStoryBeans)
    {
        List<String>  ids=new ArrayList<>();
        for (MyStoryBean myStoryBean:myStoryBeans)
        {
            if (myStoryBean.getResult()!=null&&!myStoryBean.getResult().isEmpty())
            {
                for (MyStoryBean.ResultBean resultBean:myStoryBean.getResult())
                {
                    if (resultBean.isCheckSelect())
                    {
                        ids.add(resultBean.getStoreId());
                    }
                }
            }
        }
        return ids;
    }
    /**
     * 分页查询当前用户的店铺
     *
     * @param page
     */
    public void listStore(String page,String isShow, String reviewStatus) {
        mModel.listStore(String.valueOf(AppConstant.pageSize_big), String.valueOf(page),isShow,reviewStatus).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<MyStoryBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<MyStoryBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.listStoreSuccess(listResultBean);
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
